#include <iostream>
#include <algorithm>
using namespace std;
const int N = 500 * 500 + 10;

int a, n;
int fa[N];
int pos;
int ret, cnt;
struct node
{
	int x, y, z;
}e[N];
int find(int x)
{
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}
bool cmp(node& x, node& y)
{
	return x.z < y.z;
}
void kk()
{
	sort(e + 1, e + 1 + pos, cmp);

	for (int i = 1; i <= pos; i++)
	{
		int x = e[i].x, y = e[i].y, z = e[i].z;

		int fx = find(x), fy = find(y);
		
		if (fx != fy)
		{
			cnt++;
			ret += z;
			fa[fx] = fy;
		}
	}
}
int main()
{
	cin >> a >> n;
	
	for(int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			int k; cin >> k;
			if (i >= j || k == 0 || k > a) continue;
			pos++;
			e[pos].x = i, e[pos].y = j, e[pos].z = k;
 		}
	for (int i = 1; i <= n; i++) fa[i] = i;

	kk();

	cout << ret + (n - cnt) * a << endl;
	return 0;
}